package com.kiln.juxingqs.ui

import androidx.viewpager.widget.ViewPager
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.DddjyFragmentPageAdapter
import com.kiln.juxingqs.databinding.ActivityDddjyBinding
import com.kiln.juxingqs.ui.vm.DddjyViewModel

//点对点交易
class DddjyActivity :
    BaseMiddleActivity<DddjyViewModel, ActivityDddjyBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_dddjy
    }

    val adapter: DddjyFragmentPageAdapter by lazy {
        DddjyFragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): DddjyViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel

        binding.viewPager.adapter = adapter
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                updateTab(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }


    fun clickTab(index: Int) {
        binding.viewPager.setCurrentItem(index, false)
    }

    fun updateTab(index: Int) {
        when (index) {
            0 -> {
                binding.oneTab.isSelected = true
                binding.twoTab.isSelected = false
            }
            1 -> {
                binding.oneTab.isSelected = false
                binding.twoTab.isSelected = true
            }
        }
    }

    override fun initData() {
        updateTab(0)
    }

}